package com.example.smartcity.ui.guide;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.smartcity.MainActivity;
import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.loginAdapter;
import com.example.smartcity.ui.Bean.LoginBean;
import com.example.smartcity.ui.Bean.loginList;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

public class login extends AppCompatActivity {

    private SharedPreferences sharedPreferences;
    private String TAG = "TAG";
    private ListView LV_login;
    private Button btn_login;
    String[] hint = {"请输入账号","请输入密码"};
    int[] icon = {R.drawable.phone,R.drawable.password};
    ArrayList<loginList> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        LV_login = findViewById(R.id.LV_login);
        btn_login = findViewById(R.id.btn_login);
        sharedPreferences = getSharedPreferences("First",MODE_PRIVATE);
        Boolean bol = sharedPreferences.getBoolean("IsFirst",false);
        Log.d(TAG, "onCreate: "+sharedPreferences.getBoolean("IsFirst",false));
        if (bol){
            Intent intent = new Intent(login.this, MainActivity.class);
            startActivity(intent);
        }else{
            Log.d(TAG, "onCreate: 加载");
            initView();
        }

    }

    public void initView(){
        for (int i = 0; i < hint.length; i++) {
            loginList loginList2 = new loginList(hint[i],icon[i]);
            list.add(loginList2);
        }
        LV_login.setAdapter(new loginAdapter(login.this,R.layout.loginitemlayout,list));
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                LoginBean loginBean = new LoginBean();
                SharedPreferences.Editor editor = getSharedPreferences("UwsandPws",MODE_PRIVATE).edit();
                for (int i = 0; i < hint.length; i++) {
                    EditText editText = LV_login.findViewWithTag(i);
                    switch (i){
                        case 0:
                            editor.putString("userName",editText.getText().toString());
                            loginBean.setUsername(editText.getText().toString());
                            break;
                        case 1:
                            editor.putString("pws",editText.getText().toString());
                            loginBean.setPassword(editText.getText().toString());
                            break;
                    }
                }
                editor.apply();
                getToken(loginBean);
            }
        });
    }

    public void getToken(LoginBean loginBean){
        Gson gson = new Gson();
        String json = gson.toJson(loginBean);
        String newUrl = OkHttpUtils.getBaseUrl(login.this) + "/login";
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"),json);
        OkHttpUtils.getCall(newUrl,requestBody).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: "+e.getMessage()+newUrl);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                LoginBean bean = new Gson().fromJson(response.body().string(),LoginBean.class);
                int code = bean.getCode();

                if (code==200){
                    String Token = bean.getToken();
                    if (Token.length()>0){
                        new File(login.this).saveFile("Token",Token);
                    }
                    //保存
                    SharedPreferences.Editor editor = getSharedPreferences("First",MODE_PRIVATE).edit();
                    editor.putBoolean("IsFirst",true);
                    editor.apply();
                    new File(login.this).saveFile("Token",Token);
                    //登录成功
                    Intent intent = new Intent(login.this, MainActivity.class);
                    startActivity(intent);
                }else{
                    Looper.prepare();
                    Toast.makeText(login.this, bean.getMsg(), Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }
        });
    }

    public void logon(View view){
        Intent intent = new Intent(login.this,logon.class);
        startActivity(intent);
    }

}